package com.seeyon.apps.kk.form.workflowevent;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import com.seeyon.ctp.form.bean.FormBean;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;
import com.seeyon.kk.utils.ValueUtils;

import com.google.common.collect.Maps;

/**
 *
 * 考勤确认书校验
 * 
 * @author Administrator
 *
 */
public class KaoQinQueRenShuExtendPlug extends KkBaseExtendPlug {

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return "kkKaoQinQueRenShuExtendPlug";
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "考勤确认书校验事件";
	}

	/**
	 * 返回指定的模版编号
	 * 
	 * @return
	 */
	public String getTemplateCode() {
		return "";
	};

	// 发起前事件
	public WorkflowEventResult onBeforeStart(WorkflowEventData data) {
		Map<String, Object> formdata = data.getBusinessData();
	//	FormBean formBean = (FormBean) formdata.get("formBean");

		List kqlxs = (List) formdata.get("field0011");// 考勤类型
		List memIds = (List) formdata.get("field0007"); // 姓名ID
		List startList = (List) formdata.get("field0012"); // 上班时间
		List endList = (List) formdata.get("field0036"); // 下班时间
		Map<String, Integer> memMonthNum = Maps.newHashMap();

		for (int i = 0; i < kqlxs.size(); i++) {
			Long kqenumid = (Long) kqlxs.get(i);

			if (ValueUtils.getConfig().getLong("kk.kqGsbzEnumId").equals(kqenumid)) {
				String memberid = String.valueOf(memIds.get(i));
				String start = (String) startList.get(i);
				String end = (String) endList.get(i);
				String rMonth = null;
				if (StringUtils.isNotEmpty(start)) {
					rMonth =StringUtils.substring(start,0, 7);
				} else if (StringUtils.isNotEmpty(end)) {
					rMonth = StringUtils.substring(end, 0,7);
				}

				if (StringUtils.isNotBlank(rMonth)) {
					String key = memberid + "_" + rMonth;
					Integer num = memMonthNum.get(key);
					if (num == null) {
						num = 1;
					} else {
						num = num + 1;
					}
					memMonthNum.put(key, num);
				}
			}

		}

		for (Iterator<String> iterator = memMonthNum.keySet().iterator(); iterator.hasNext();) {
			String mkey = iterator.next();
			String[] keys = StringUtils.split(mkey, "_");
			Map<String, Object> pm = Maps.newHashMap();
			pm.put("memberId", keys[0]);
			pm.put("month", keys[1]);
			pm.put("kqGsbzEnumId", ValueUtils.getConfig().getLong("kk.kqGsbzEnumId"));
			String sql = freemarkFlowManager.fltTemplateIntoString("sql/hr/KQQRD.ftl", pm);
			List<Map<String, Object>> vl = this.kkJdbcTemplate.queryForList(sql);
			int num = memMonthNum.get(mkey) + vl.size();
			memMonthNum.put(mkey, num);
		}

		for (Iterator<String> iterator = memMonthNum.keySet().iterator(); iterator.hasNext();) {
			String mkey = iterator.next();

			if (memMonthNum.get(mkey) > 3) {
				String[] keys = StringUtils.split(mkey, "_");

				try {
					V3xOrgMember v3m = this.orgManager.getMemberById(Long.valueOf(keys[0]));
					WorkflowEventResult er = new WorkflowEventResult();
					er.setAlertMessage(v3m.getName() + " 工时补足 " + keys[1] + " 累计超过 " + 3 + "次！");
					return er;
				} catch (Exception e) {
					e.printStackTrace();
					WorkflowEventResult er = new WorkflowEventResult();
					er.setAlertMessage("校验出错不允许提交！");
					return er;
				}
			}
		}

		return null;
	}

}
